package xin.yangshuai.jvm01.reference;

import java.lang.ref.WeakReference;
import java.util.WeakHashMap;

public class Hello {

//    private int[] array = new int[1024];

    private String name;

    private int age;

    public Hello() {
    }

    public Hello(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Hello{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public static void main(String[] args) {
        Hello hello = new Hello("123", 456);
        WeakReference<Hello> helloWeakReference = new WeakReference<>(hello);
        hello = null;

//        Hello x = helloWeakReference.get();

        System.out.println(helloWeakReference.get().age);

        WeakHashMap<String, String> map = new WeakHashMap<>();

        map.put(new String("a"),"a");
        map.put("b","b");

        System.out.println(map.get("a"));
        System.out.println(map.get("b"));

        try {
            System.gc();
        } catch (Exception ignored) {

        }

        System.out.println(helloWeakReference.get());

        System.out.println(map.get("a"));
        System.out.println(map.get("b"));


    }
}